package com.yunhe.authority.repository.base;
import com.yunhe.authority.domain.base.MessageCenter;
import com.yunhe.authority.model.base.WhetherReadQueryCondition;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @description 消息中心
 * @author misty
 * @date 2021-10-18
 */
@Repository
public interface MessageCenterRepository extends Neo4jRepository<MessageCenter,Long> {


    @Query(value = "MATCH(mc:MessageCenter) where mc.messageSendType = '01' and  mc.messagePerson={headerUserId} " +
            "and mc.messageDestination={messageDestination} and mc.personType={personType} return mc order by mc.whetherRead asc,mc.messageTime desc",
    countQuery = "MATCH(mc:MessageCenter) where mc.messageSendType = '01' and  mc.messagePerson={headerUserId} and " +
            "mc.messageDestination={messageDestination} and mc.personType={personType} return count(mc)")
    Page<MessageCenter> findMessageByCondition(@Param("headerUserId") Long headerUserId,
                                                   @Param("messageDestination") String messageDestination,
                                                   @Param("personType") String personType, Pageable pageable);

    @Query(value = "MATCH(mc:MessageCenter) where mc.messageSendType = '01' and  ((mc.companyId={companyId} and mc.messagePerson is null) or (mc.companyId={companyId} and mc.messagePerson ={messagePerson})) " +
            "and mc.messageDestination={messageDestination} and mc.personType={personType} return mc order by mc.whetherRead asc,mc.messageTime desc",
            countQuery = "MATCH(mc:MessageCenter) where mc.messageSendType = '01' and  ((mc.companyId={companyId} and mc.messagePerson is null) or (mc.companyId={companyId} and mc.messagePerson ={messagePerson})) " +
                    "and mc.messageDestination={messageDestination} and mc.personType={personType} return count(mc)")
    Page<MessageCenter>  findWeChatMessage(@Param("companyId") Long companyId,
                                           @Param("messageDestination") String messageDestination,
                                           @Param("personType") String personType,@Param("messagePerson") Long userId, Pageable pageable);

    MessageCenter findMessageByBusinessIdAndMessageTypeAndMessagePerson(Long businessId, String messageType, Long messagePerson);

    MessageCenter findMessageByBusinessIdAndMessageTypeAndCompanyId(Long businessId, String messageType, Long companyId);

    List<MessageCenter> findMessageByBusinessIdAndMessageType(Long businessId, String messageType);



    @Query(value = "MATCH(mc:MessageCenter) where mc.messageType = {messageType} and  mc.businessId={businessId} " +
            "and mc.whetherRead={whetherRead} return mc ",
            countQuery = "MATCH(mc:MessageCenter) where mc.messageType = {messageType} and  mc.businessId={businessId} " +
                    "and mc.whetherRead={whetherRead} return count(mc)")
    Page<MessageCenter> findMessageByConditionForWeb(@Param("whetherRead") String whetherRead,
                                                     @Param("businessId") Long businessId,
                                                     @Param("messageType") String messageType, Pageable pageable);
}